package com.ict.ais.realTime.service.api.impl;


import common.SouTypeDeal;
import org.springframework.stereotype.Service;

import com.ict.ais.realTime.facade.api.IRealTimeMMSIService;
import com.ict.ais.realTime.facade.dto.CountryTPCount;
import com.ict.ais.realTime.facade.dto.RealAreaParams;
import com.ict.ais.realTime.facade.dto.ResultObj;
import com.ict.ais.realTime.facade.model.AppointTarget;
import com.ict.ais.realTime.facade.model.AppointTargetFuseRst;
import com.ict.ais.realTime.facade.model.FuseTargetFromSource;
import com.ict.ais.realTime.facade.model.GridShips;
import com.ict.ais.realTime.facade.model.GridShipsCount;
import com.ict.ais.realTime.facade.model.GridShipsCountRst;
import com.ict.ais.realTime.facade.model.RealTimeMMSI;
import com.ict.ais.realTime.facade.model.ShipBaseInfo;
import com.ict.ais.realTime.facade.model.TypeCount;
import com.ict.ais.realTime.service.dao.IRealTimeMMSIDao;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("realTimeMMSIService")
public class RealTimeMMSIService implements IRealTimeMMSIService{
    @Resource
    IRealTimeMMSIDao iRealTimeMMSI;

    public List<RealTimeMMSI> getDegInfo(double ldlon, double ldlat, double rulon, double rulat, int limit, int timeout, int mode) {
        RealAreaParams realAreaParams = new RealAreaParams();
        realAreaParams.setLdlat((int) (ldlat * 600000));
        realAreaParams.setLdlon((int) (ldlon * 600000));
        realAreaParams.setRulat((int) (rulat * 600000));
        realAreaParams.setRulon((int) (rulon * 600000));
        realAreaParams.setLim(limit);
        realAreaParams.setTimout(timeout);
        List<RealTimeMMSI> realTimeMMSIList;
        try {
            List<RealTimeMMSI> args;
            if (mode == 0) {
                if (timeout == 0) {
                    args = iRealTimeMMSI.getFuseDegInfotime0(realAreaParams);
                } else {
                    args = iRealTimeMMSI.getFuseDegInfo(realAreaParams);
                }
                realTimeMMSIList = cutUnknownStr(args);
                return realTimeMMSIList;
            } else if (mode == 1) {
                if (timeout == 0) {
                    args = iRealTimeMMSI.getOriginDegInfo0(realAreaParams);
                } else {
                    args = iRealTimeMMSI.getOriginDegInfo(realAreaParams);
                }
                realTimeMMSIList = cutUnknownStr(args);
                return realTimeMMSIList;
            } else {
                return null;
            }
        } catch (Exception e) {
            System.out.print("数据操作异常：" + e);
            return null;
        }
    }

    /**
     * 重点目标
     * @param limit
     * @param timeout
     * @return
     */
    public ResultObj getImportantTarget(int limit, int timeout){
        ResultObj resultObj=new ResultObj();
        List<RealTimeMMSI> realTimeMMSIList;
        try{
            List<RealTimeMMSI> args=null;
            if (timeout == 0) {
                args = iRealTimeMMSI.getFuseImTargetWithouLimit();
            } else {
                args = iRealTimeMMSI.getFuseImTargetLimit(limit);
            }

            realTimeMMSIList = cutUnknownStr(args);

            if(realTimeMMSIList.size()<1){
                resultObj.setState(0);
                return resultObj;
            }else {
                resultObj.setState(1);
                resultObj.setObj(realTimeMMSIList);
                return resultObj;
            }
        }catch (Exception e){
            System.out.println("数据库操作失败："+e);
            resultObj.setState(-1);
            return resultObj;
        }
    }

    /**
     * 获取指定船舶信息
     *
     * @param num
     * @param mode
     * @return
     */
    public AppointTarget getAppointTarget(int num, int mode) {
        try {
            if (mode == 0) {
                AppointTarget appointTarget = iRealTimeMMSI.getFuseAppointTarget(num);
                if (appointTarget != null) {
                    appointTarget = cutOOUnknown(appointTarget);
                    return appointTarget;
                } else {
                    return appointTarget;
                }
            } else {
                int[] modeArr= SouTypeDeal.getSourceAndType(mode);
                AppointTarget appointTarget = iRealTimeMMSI.getOriginAppointTarget(num, modeArr[0],modeArr[1]);
                if (appointTarget != null) {
                    appointTarget = cutOOUnknown(appointTarget);
                    return appointTarget;
                } else {
                    return appointTarget;
                }
            }
        } catch (Exception e) {
            System.out.print("数据操作异常：" + e);
            return null;
        }
    }

    public ResultObj getwarsshipositionFuse(int mmsi){
        try {
            List<AppointTarget> appointTargetList = iRealTimeMMSI.getFuseWarShips(mmsi);
            if (appointTargetList.size() >0) {

                /// TODO: 2017/10/20  
                
                for (int k=0;k<appointTargetList.size();k++){
                    AppointTarget appointTarget=appointTargetList.get(k);

                    appointTarget = cutOOUnknown(appointTarget);

                    List<FuseTargetFromSource> fuseTargetFromSourceList = iRealTimeMMSI.getListWarShipsByFuse(mmsi);
                    List<Integer> orig_info_typeList = new ArrayList<Integer>();
                    List<Integer> orig_info_sourceList = new ArrayList<Integer>();
                    List<Integer> target_id_origList = new ArrayList<Integer>();
                    List<Integer> targetIDOrig_TypeList = new ArrayList<Integer>();

                    if (fuseTargetFromSourceList.size() > 0) {
                        //融合表里面能查到融合记录
                        for (int i = 0; i < fuseTargetFromSourceList.size(); i++) {
                            orig_info_typeList.add(fuseTargetFromSourceList.get(i).getOrig_info_type());
                            orig_info_sourceList.add(fuseTargetFromSourceList.get(i).getOrig_info_source());
                            target_id_origList.add(fuseTargetFromSourceList.get(i).getTarget_id_orig());
                            targetIDOrig_TypeList.add(fuseTargetFromSourceList.get(i).getTargetIDOrig_Type());
                        }
                    } else {
                        //融合表里面能查不到融合记录
                        orig_info_typeList.add(appointTarget.getOrig_info_type());
                        orig_info_sourceList.add(appointTarget.getOrig_info_source());
                        target_id_origList.add(appointTarget.getTarget_id_orig());
                        targetIDOrig_TypeList.add(appointTarget.getTargetIDOrig_Type());
                    }

                    AppointTargetFuseRst appointTargetFuseRst = new AppointTargetFuseRst();
                    appointTargetFuseRst.setTarget_id(appointTarget.getTarget_id());
                    appointTargetFuseRst.setRecord_utc_time(appointTarget.getRecord_utc_time());
                    appointTargetFuseRst.setLongitude(appointTarget.getLongitude());
                    appointTargetFuseRst.setLatitude(appointTarget.getLatitude());
                    appointTargetFuseRst.setDirection(appointTarget.getDirection());
                    appointTargetFuseRst.setHeading(appointTarget.getHeading());
                    appointTargetFuseRst.setSpeed(appointTarget.getSpeed());
                    appointTargetFuseRst.setStatus(appointTarget.getStatus());
                    appointTargetFuseRst.setRot_sensor(appointTarget.getRot_sensor());
                    appointTargetFuseRst.setMmsi(appointTarget.getMmsi());
                    appointTargetFuseRst.setShip_name(appointTarget.getShip_name());
                    appointTargetFuseRst.setCall_sign(appointTarget.getCall_sign());
                    appointTargetFuseRst.setImo_number(appointTarget.getImo_number());
                    appointTargetFuseRst.setDestination(appointTarget.getDestination());
                    appointTargetFuseRst.setEta(appointTarget.getEta());
                    appointTargetFuseRst.setDraft(appointTarget.getDraft());
                    appointTargetFuseRst.setCountry_name(appointTarget.getCountry_name());
                    appointTargetFuseRst.setShip_type(appointTarget.getShip_type());
                    appointTargetFuseRst.setCargo_type(appointTarget.getCargo_type());
                    appointTargetFuseRst.setShip_length(appointTarget.getShip_length());
                    appointTargetFuseRst.setShip_breadth(appointTarget.getShip_breadth());
                    appointTargetFuseRst.setFixing_device(appointTarget.getFixing_device());
                    appointTargetFuseRst.setPos_accuracy(appointTarget.getPos_accuracy());
                    appointTargetFuseRst.setCommun_state(appointTarget.getCommun_state());
                    appointTargetFuseRst.setOrig_info_source(orig_info_sourceList);
                    appointTargetFuseRst.setOrig_info_type(orig_info_typeList);
                    appointTargetFuseRst.setTargetIDOrig_Type(targetIDOrig_TypeList);
                    appointTargetFuseRst.setTarget_id_orig(target_id_origList);
                    
                }

            } else {
                /// TODO: 2017/10/20  
                return null;
            }
        } catch (Exception e) {
            System.out.print("数据操作异常：" + e);
            return null;
        }
        /// TODO: 2017/10/20  
        return null;
    }

    /**
     * 获取综合模式下指定船舶信息，带来源及ID
     *
     * @param num
     * @return
     */
    public AppointTargetFuseRst getAppointTargetSource(int num) {
        try {
            AppointTarget appointTarget = iRealTimeMMSI.getFuseAppointTarget(num);
            if (appointTarget != null) {
                appointTarget = cutOOUnknown(appointTarget);

                List<FuseTargetFromSource> fuseTargetFromSourceList = iRealTimeMMSI.getListOrigTargetByFuse(num);
                List<Integer> orig_info_typeList = new ArrayList<Integer>();
                List<Integer> orig_info_sourceList = new ArrayList<Integer>();
                List<Integer> target_id_origList = new ArrayList<Integer>();
                List<Integer> targetIDOrig_TypeList = new ArrayList<Integer>();

                if (fuseTargetFromSourceList.size() > 0) {
                    //融合表里面能查到融合记录
                    for (int i = 0; i < fuseTargetFromSourceList.size(); i++) {
                        orig_info_typeList.add(fuseTargetFromSourceList.get(i).getOrig_info_type());
                        orig_info_sourceList.add(fuseTargetFromSourceList.get(i).getOrig_info_source());
                        target_id_origList.add(fuseTargetFromSourceList.get(i).getTarget_id_orig());
                        targetIDOrig_TypeList.add(fuseTargetFromSourceList.get(i).getTargetIDOrig_Type());
                    }
                } else {
                    //融合表里面能查不到融合记录
                    orig_info_typeList.add(appointTarget.getOrig_info_type());
                    orig_info_sourceList.add(appointTarget.getOrig_info_source());
                    target_id_origList.add(appointTarget.getTarget_id_orig());
                    targetIDOrig_TypeList.add(appointTarget.getTargetIDOrig_Type());
                }

                AppointTargetFuseRst appointTargetFuseRst = new AppointTargetFuseRst();
                appointTargetFuseRst.setTarget_id(appointTarget.getTarget_id());
                appointTargetFuseRst.setRecord_utc_time(appointTarget.getRecord_utc_time());
                appointTargetFuseRst.setLongitude(appointTarget.getLongitude());
                appointTargetFuseRst.setLatitude(appointTarget.getLatitude());
                appointTargetFuseRst.setDirection(appointTarget.getDirection());
                appointTargetFuseRst.setHeading(appointTarget.getHeading());
                appointTargetFuseRst.setSpeed(appointTarget.getSpeed());
                appointTargetFuseRst.setStatus(appointTarget.getStatus());
                appointTargetFuseRst.setRot_sensor(appointTarget.getRot_sensor());
                appointTargetFuseRst.setMmsi(appointTarget.getMmsi());
                appointTargetFuseRst.setShip_name(appointTarget.getShip_name());
                appointTargetFuseRst.setCall_sign(appointTarget.getCall_sign());
                appointTargetFuseRst.setImo_number(appointTarget.getImo_number());
                appointTargetFuseRst.setDestination(appointTarget.getDestination());
                appointTargetFuseRst.setEta(appointTarget.getEta());
                appointTargetFuseRst.setDraft(appointTarget.getDraft());
                appointTargetFuseRst.setCountry_name(appointTarget.getCountry_name());
                appointTargetFuseRst.setShip_type(appointTarget.getShip_type());
                appointTargetFuseRst.setCargo_type(appointTarget.getCargo_type());
                appointTargetFuseRst.setShip_length(appointTarget.getShip_length());
                appointTargetFuseRst.setShip_breadth(appointTarget.getShip_breadth());
                appointTargetFuseRst.setFixing_device(appointTarget.getFixing_device());
                appointTargetFuseRst.setPos_accuracy(appointTarget.getPos_accuracy());
                appointTargetFuseRst.setCommun_state(appointTarget.getCommun_state());
                appointTargetFuseRst.setOrig_info_source(orig_info_sourceList);
                appointTargetFuseRst.setOrig_info_type(orig_info_typeList);
                appointTargetFuseRst.setTargetIDOrig_Type(targetIDOrig_TypeList);
                appointTargetFuseRst.setTarget_id_orig(target_id_origList);

                return appointTargetFuseRst;
            } else {
                return null;
            }
        } catch (Exception e) {
            System.out.print("数据操作异常：" + e);
            return null;
        }
    }

    /**
     * 获取目标档案信息
     *
     * @param shipid
     * @return
     */
    public ResultObj getListShipBaseInfo(String shipid, String shiptype) {
        ResultObj resultObj = new ResultObj();
        List<ShipBaseInfo> shipBaseInfoList = new ArrayList<ShipBaseInfo>();
        try {
            String[] strArrayShipID = Split_StrArray(shipid);
            String[] strArrayShipType = Split_StrArray(shiptype);
            for (int i = 0; i < strArrayShipID.length; i++) {
                ShipBaseInfo shipBaseInfo = iRealTimeMMSI.getOrigListShipBaseInfo(Integer.parseInt(strArrayShipID[i]), Integer.parseInt(strArrayShipType[i]));
                if (shipBaseInfo != null) {
                    shipBaseInfoList.add(shipBaseInfo);
                }
            }
            if (shipBaseInfoList.size() == 0) {
                resultObj.setState(0);
                return resultObj;
            } else {
                resultObj.setState(1);
                resultObj.setObj(shipBaseInfoList);
                return resultObj;
            }
        } catch (Exception e) {
            System.out.println("数据库操作失败:" + e);
            resultObj.setState(-1);
            return resultObj;
        }
    }

    public String[] Split_StrArray(String str) throws Exception {
        String[] strArray = str.split("~");
//        String s = strArray[0].toString();
//        System.out.println(s);
        return strArray;
    }

    public List<AppointTarget> realTimeTargetGet() {
        try {
            return iRealTimeMMSI.realTimeTargetGet();
        } catch (Exception e) {
            System.out.print("数据操作异常：" + e);
            return null;
        }
    }

    /**
     * 实时区域统计
     *
     * @param ldlon
     * @param ldlat
     * @param rulon
     * @param rulat
     * @param mode  0：融合模式，1：原始模式
     * @return
     */
    public List<CountryTPCount> getSquAreaCount(double ldlon, double ldlat, double rulon, double rulat, int mode) {
        try {
            String[] countryArr = {"中国", "英国", "美国", "法国", "俄罗斯"};
            List<CountryTPCount> countryTPCountList = new ArrayList<CountryTPCount>();

            for (int i = 0; i < countryArr.length; i++) {
                List<TypeCount> typeCountList;
                if (mode == 0) {
                    typeCountList = iRealTimeMMSI.getSquFuseAreaCount(countryArr[i], ldlon, ldlat, rulon, rulat);
                } else if (mode == 1) {
                    typeCountList = iRealTimeMMSI.getSquOrigAreaCount(countryArr[i], ldlon, ldlat, rulon, rulat);
                } else {
                    return null;
                }

                CountryTPCount countryTPCount = new CountryTPCount();
                countryTPCount.setCo(countryArr[i]);
                int count2 = 0;

                for (int j = 0; j < typeCountList.size(); j++) {
                    /**
                     * 1; //货船
                     * 2; //Search And Rescue船，搜救船
                     * 3; //油轮
                     * 4; //拖轮
                     * 5; //渔船
                     * 6; //拖船（拖网作业）
                     * 7; //客船
                     * 8://军事行动船
                     * 9://渔政等执法船
                     * 10://风效应船
                     * 11://高速船
                     * 100; //其他类型船
                     */
                    switch (typeCountList.get(j).getTps()) {
                        case 1:
                            countryTPCount.setTc1(typeCountList.get(j).getCounts());
                            break;
                        case 2:
                            countryTPCount.setTc2(typeCountList.get(j).getCounts());
                            break;
                        case 3:
                            countryTPCount.setTc3(typeCountList.get(j).getCounts());
                            break;
                        case 4:
                            countryTPCount.setTc4(typeCountList.get(j).getCounts());
                            break;
                        case 5:
                            countryTPCount.setTc5(typeCountList.get(j).getCounts());
                            break;
                        case 6:
                            countryTPCount.setTc6(typeCountList.get(j).getCounts());
                            break;
                        case 7:
                            countryTPCount.setTc7(typeCountList.get(j).getCounts());
                            break;
                        //fcc修改
                        case 8:
                            countryTPCount.setTc7(typeCountList.get(j).getCounts());
                            break;
                        case 9:
                            countryTPCount.setTc7(typeCountList.get(j).getCounts());
                            break;
                        case 10:
                            countryTPCount.setTc7(typeCountList.get(j).getCounts());
                            break;
                        case 11:
                            countryTPCount.setTc7(typeCountList.get(j).getCounts());
                            break;
                        //fcc修改
                        //其他类型船舶需要处理
                        default:
                            //因为其他类型还有多种，所以需要循环相加
                            count2 += typeCountList.get(j).getCounts();
                            break;

                    }
                }

                countryTPCount.setTc100(count2);
                countryTPCountList.add(countryTPCount);
            }

            //统计其他
            String country = "其他";
            List<TypeCount> typeCountList2;
            if (mode == 0) {
                typeCountList2 = iRealTimeMMSI.getSquEleFuseAreaCount(ldlon, ldlat, rulon, rulat);
            } else if (mode == 1) {
                typeCountList2 = iRealTimeMMSI.getSquEleOrigAreaCount(ldlon, ldlat, rulon, rulat);
            } else {
                return null;
            }

            CountryTPCount countryTPCount2 = new CountryTPCount();
            countryTPCount2.setCo(country);

            int coun = 0;
            for (int j = 0; j < typeCountList2.size(); j++) {
                switch (typeCountList2.get(j).getTps()) {
                    case 1:
                        countryTPCount2.setTc1(typeCountList2.get(j).getCounts());
                        break;
                    case 2:
                        countryTPCount2.setTc2(typeCountList2.get(j).getCounts());
                        break;
                    case 3:
                        countryTPCount2.setTc3(typeCountList2.get(j).getCounts());
                        break;
                    case 4:
                        countryTPCount2.setTc4(typeCountList2.get(j).getCounts());
                        break;
                    case 5:
                        countryTPCount2.setTc5(typeCountList2.get(j).getCounts());
                        break;
                    case 6:
                        countryTPCount2.setTc6(typeCountList2.get(j).getCounts());
                        break;
                    case 7:
                        countryTPCount2.setTc7(typeCountList2.get(j).getCounts());
                        break;
                    //fcc修改
                    case 8:
                        countryTPCount2.setTc7(typeCountList2.get(j).getCounts());
                        break;
                    case 9:
                        countryTPCount2.setTc7(typeCountList2.get(j).getCounts());
                        break;
                    case 10:
                        countryTPCount2.setTc7(typeCountList2.get(j).getCounts());
                        break;
                    case 11:
                        countryTPCount2.setTc7(typeCountList2.get(j).getCounts());
                        break;
                    //fcc修改
                    //其他类型船舶需要处理
                    default:
                        //因为其他类型还有多种，所以需要循环相加
                        coun += typeCountList2.get(j).getCounts();
                        break;
                }
            }

            countryTPCount2.setTc100(coun);
            countryTPCountList.add(countryTPCount2);
            return countryTPCountList;
        } catch (Exception e) {
            System.out.println("数据操作异常" + e);
            return null;
        }
    }

    /**
     * 获取区域目标总数
     *
     * @param ldlon
     * @param ldlat
     * @param rulon
     * @param rulat
     * @param mode  0：融合模式，1：原始模式
     * @return
     */
    public int areaCount(double ldlon, double ldlat, double rulon, double rulat, int mode) {
        try {
            if (mode == 0) {
                return iRealTimeMMSI.fuseAreaCount(ldlon, ldlat, rulon, rulat);
            } else if (mode == 1) {
                return iRealTimeMMSI.origAreaCount(ldlon, ldlat, rulon, rulat);
            } else {
                return -2;
            }
        } catch (Exception e) {
            return -1;
        }
    }

    //单目标去除@@@参数
    private AppointTarget cutOOUnknown(AppointTarget arg) {
        String strCN = "";
        String strDes = "";
        String strSN = "";
        String strCS = "";
        if (!arg.getCountry_name().equals("")) {
            strCN = arg.getCountry_name().substring(0, 1);
        }
        if (!arg.getDestination().equals("")) {
            strDes = arg.getDestination().substring(0, 1);
        }
        if (!arg.getShip_name().equals("")) {
            arg.getShip_name().substring(0, 1);
        }
        if (!arg.getCall_sign().equals("")) {
            arg.getCall_sign().substring(0, 1);
        }
        if (strCN.equals("@")) {
            arg.setCountry_name("");
        }
        if (strDes.equals("@")) {
            arg.setDestination("");
        }
        if (strSN.equals("@")) {
            arg.setShip_name("");
        }
        if (strCS.equals("@")) {
            arg.setCall_sign("");
        }
        return arg;
    }

    //去除返回中@@@字符串
    private List<RealTimeMMSI> cutUnknownStr(List<RealTimeMMSI> args) {
        List<RealTimeMMSI> list = new ArrayList<RealTimeMMSI>();
        String sT = "@";
        for (RealTimeMMSI rea : args) {
            RealTimeMMSI arg = new RealTimeMMSI();
            if (!rea.getCo().equals("")) {
                sT = rea.getCo().substring(0, 1);
            }
            if (sT.equals("@")) {
                arg.setCo("");
            } else {
                arg.setCo(rea.getCo());
            }
            arg.setDi(rea.getDi());
            arg.setHe(rea.getHe());
            arg.setLa(rea.getLa());
            arg.setLo(rea.getLo());
            arg.setMs(rea.getMs());
            arg.setMt(rea.getMt());
            arg.setNu(rea.getNu());
            arg.setIit(rea.getIit());
            arg.setMsi(rea.getMsi());
            arg.setTl(rea.getTl());
            arg.setImo(rea.getImo());
            if (!rea.getSn().equals("")) {
                sT = rea.getSn().substring(0, 1);
            }
            if (sT.equals("@")) {
                arg.setSn("");
            } else {
                arg.setSn(rea.getSn());
            }
            arg.setSp(rea.getSp());
            arg.setSs(rea.getSs());
            arg.setSt(rea.getSt());
            arg.setTi(rea.getTi());

            if (rea.getCas()==null){
                arg.setCas("");
            }else
            {
                arg.setCas(rea.getCas());
            }
            list.add(arg);
        }
        return list;
    }

    /**
     * 获取栅格船舶
     *
     * @return
     */
    public ResultObj getGridShips(Integer timeout, int mode) {
        ResultObj resultObj = new ResultObj();
        int lo, la;
        double lodou,ladou;
        String key;
        Map<String, Integer> map = new HashMap<String, Integer>();

        List<GridShips> gridShipsList = new ArrayList<GridShips>();
        if (mode == 0) {
            try {
                if(timeout==0){
                    gridShipsList = iRealTimeMMSI.getFuseGridShips();
                }else{
                    gridShipsList = iRealTimeMMSI.getFuseGridShipsLimit(timeout);
                }
            } catch (Exception e) {
                System.out.println("数据库操作失败：" + e);
            }
        } else if (mode == 1) {
            try {
                if(timeout==0){
                    gridShipsList = iRealTimeMMSI.getOrigGridShips();
                }else {
                    gridShipsList = iRealTimeMMSI.getOrigGridShipsLimit(timeout);
                }
            } catch (Exception e) {
                System.out.println("数据库操作失败：" + e);
            }

        } else {
            gridShipsList = null;
        }

        if (gridShipsList.size() == 0) {
            resultObj.setState(0);
            return resultObj;
        } else {
            for (int i = 0; i < gridShipsList.size(); i++) {
                lo = gridShipsList.get(i).getLo();
                la = gridShipsList.get(i).getLa();
                lo = (int)Math.round(lo / 600000.0);
                la = (int)Math.round(la / 600000.0);
                key = lo + "_" + la;
                hashShipCount(map, key);
            }
            List<GridShipsCount> gridShipsCountList = new ArrayList<GridShipsCount>();
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                gridShipsCountList.add(mapKeyValueToObj(entry.getKey(), entry.getValue()));
            }

            resultObj.setState(1);
            resultObj.setObj(gridShipsCountList);
            return resultObj;
        }
    }

    /**
     * @param key
     * @param value
     * @return
     */
    public GridShipsCount mapKeyValueToObj(String key, int value) {
        GridShipsCount gridShipsCount = new GridShipsCount();
        String[] keyLonLat = key.split("_");
        gridShipsCount.setLon(Integer.parseInt(keyLonLat[0]));
        gridShipsCount.setLat(Integer.parseInt(keyLonLat[1]));
        gridShipsCount.setV(value);
        return gridShipsCount;
    }

    /**
     * 栅格统计
     *
     * @return
     */
    public void hashShipCount(Map<String, Integer> map, String key) {
        if (!map.containsKey(key)) {
            map.put(key, 0);
        }
        map.put(key, map.get(key).intValue() + 1);
    }


    /**
     * 获取栅格船舶速度和
     *
     * @return
     */
    public ResultObj getGridShipsSumSpeed(Integer timeout, int mode) {
        ResultObj resultObj = new ResultObj();
        int lo, la, sp;
        String key;
        Map<String, Integer> map = new HashMap<String, Integer>();

        List<GridShips> gridShipsList = new ArrayList<GridShips>();
        if (mode == 0) {
            try {
                if(timeout==0){
                    gridShipsList = iRealTimeMMSI.getFuseGridShips();
                }else {
                    gridShipsList = iRealTimeMMSI.getFuseGridShipsLimit(timeout);
                }
            } catch (Exception e) {
                System.out.println("数据库操作失败：" + e);
            }
        } else if (mode == 1) {
            try {
                if(timeout==0){
                    gridShipsList = iRealTimeMMSI.getOrigGridShips();
                }else {
                    gridShipsList = iRealTimeMMSI.getOrigGridShipsLimit(timeout);
                }
            } catch (Exception e) {
                System.out.println("数据库操作失败：" + e);
            }

        } else {
            gridShipsList = null;
        }

        if (gridShipsList.size() == 0) {
            resultObj.setState(0);
            return resultObj;
        } else {
            for (int i = 0; i < gridShipsList.size(); i++) {
                lo = gridShipsList.get(i).getLo();
                la = gridShipsList.get(i).getLa();
                lo = lo / 600000;
                la = la / 600000;
                key = lo + "_" + la;
                sp = gridShipsList.get(i).getSp();
                hashShipCountSumSpeed(map, key, sp);
            }
            List<GridShipsCount> gridShipsCountList = new ArrayList<GridShipsCount>();
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                gridShipsCountList.add(mapKeyValueToObj(entry.getKey(), entry.getValue()));
            }

            resultObj.setState(1);
            resultObj.setObj(gridShipsCountList);
            return resultObj;
        }
    }

    /**
     * 速度和统计
     *
     * @return
     */
    public void hashShipCountSumSpeed(Map<String, Integer> map, String key, int speed) {
        if (!map.containsKey(key)) {
            map.put(key, 0);
        }
        map.put(key, map.get(key).intValue() + speed);
    }

    /**
     * 获取栅格船舶速度平均值统计
     *
     * @return
     */
    public ResultObj getGridShipsAvgSpeed(Integer timeout, int mode) {
        ResultObj resultObj = new ResultObj();
        int lo, la, sp;
        String key;
        Map<String, Integer> map = new HashMap<String, Integer>();
        Map<String, Integer> map2 = new HashMap<String, Integer>();

        List<GridShips> gridShipsList = new ArrayList<GridShips>();
        if (mode == 0) {
            try {
                if(timeout==0){
                    gridShipsList = iRealTimeMMSI.getFuseGridShips();
                }else {
                    gridShipsList = iRealTimeMMSI.getFuseGridShipsLimit(timeout);
                }
            } catch (Exception e) {
                System.out.println("数据库操作失败：" + e);
            }
        } else if (mode == 1) {
            try {
                if (timeout==0){
                    gridShipsList = iRealTimeMMSI.getOrigGridShips();
                }else{
                    gridShipsList = iRealTimeMMSI.getOrigGridShipsLimit(timeout);
                }
            } catch (Exception e) {
                System.out.println("数据库操作失败：" + e);
            }

        } else {
            gridShipsList = null;
        }

        if (gridShipsList.size() == 0) {
            resultObj.setState(0);
            return resultObj;
        } else {
            try {
                for (int i = 0; i < gridShipsList.size(); i++) {
                    lo = gridShipsList.get(i).getLo();
                    la = gridShipsList.get(i).getLa();
                    lo = lo / 600000;
                    la = la / 600000;
                    key = lo + "_" + la;
                    sp = gridShipsList.get(i).getSp();

                    hashShipCountSumSpeed(map, key, sp);
                    hashShipCount(map2, key);
                }

                List<GridShipsCount> gridShipsCountList = new ArrayList<GridShipsCount>();
                List<GridShipsCount> gridShipsCountList2 = new ArrayList<GridShipsCount>();

                List<GridShipsCountRst> gridShipsCountRsts = new ArrayList<GridShipsCountRst>();

                for (Map.Entry<String, Integer> entry : map.entrySet()) {
                    gridShipsCountList.add(mapKeyValueToObj(entry.getKey(), entry.getValue()));
                }

                for (Map.Entry<String, Integer> entry2 : map2.entrySet()) {
                    gridShipsCountList2.add(mapKeyValueToObj(entry2.getKey(), entry2.getValue()));
                }

                for (int i = 0; i < gridShipsCountList.size(); i++) {
                    for (int j = 0; j < gridShipsCountList2.size(); j++) {
                        if (gridShipsCountList.get(i).getLon() == gridShipsCountList2.get(j).getLon()
                                && gridShipsCountList.get(i).getLat() == gridShipsCountList2.get(j).getLat()) {
                            GridShipsCountRst gridShipsCountRst = new GridShipsCountRst();
                            gridShipsCountRst.setLon(gridShipsCountList.get(i).getLon());
                            gridShipsCountRst.setLat(gridShipsCountList.get(i).getLat());

                            gridShipsCountRst.setV((gridShipsCountList.get(i)).getV() / (gridShipsCountList2.get(j).getV()));
                            gridShipsCountRsts.add(gridShipsCountRst);
                        }
                    }
                }

                resultObj.setState(1);
                resultObj.setObj(gridShipsCountRsts);
                return resultObj;
            } catch (Exception e) {
                System.out.println("数据操作失败：" + e);
                resultObj.setState(-1);
                return resultObj;
            }
        }
    }
}